package testMybatis;

import com.chinaunicom.mybatis.mapper.AnswerMapper;
import com.chinaunicom.mybatis.model.Answer;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import static testMybatis.BizUtil.createAnswer;

/**
 * Created by houjinxin on 16/9/3.
 */
public class InsertDBMultiTimeTask implements Runnable {

    private int taskId;
    private CountDownLatch count;
    private AnswerMapper answerMapper;
    private List<Answer> answers;
    private int loopTimes;

    public InsertDBMultiTimeTask(int taskId, CountDownLatch count, AnswerMapper answerMapper, List<Answer> answers, int loopTimes) {
        this.taskId = taskId;
        this.count = count;
        this.answerMapper = answerMapper;
        this.answers = answers;
        this.loopTimes = loopTimes;

    }

    @Override
    public void run() {
        try {
            for (int j = 0; j < loopTimes; j++) {
                long start = System.currentTimeMillis();
                answerMapper.insertBatch(answers);
                long end = System.currentTimeMillis();
                System.out.println("任务"+taskId+"：第" + (j + 1) + "次批量插入数据耗时间为：" + ((end - start) / 1000) + "s");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            count.countDown();
        }
    }
}
